#include "httpServer.hpp"
#include "session.hpp"

std::string Uip = "192.168.233.136", Uport;

void Login(httpRuest &req, httpResponse &resp)
{
    // 根据req, 动态的构建 resq
    LOG(LogLevel::DEBUG) << "进入到了登录的模块" << req.Path() << ", " << req.Args();
    string req_args = req.Args();

    // 1. 解析参数格式，得到想要的参数

    // 2. 访问数据库， 验证对应的用户是否为合法的用户
    // todo:
    sessionManager m;
    Session id = m.createSession(1);

    // 3. 登录成功
    string body = req.getContent("./wwwroot/logonSuccess.html");
    resp.setCode(200);
    resp.setHeader("Content-Length", std::to_string(body.size()));
    resp.setHeader("Content-Type", "text/html");
    resp.setBody(body);
    // sleep(3);
    // resp.setCode(302);
    // resp.setHeader("Location", "/");
}
void Register(httpRuest &req, httpResponse &resq)
{
    // 根据req, 动态的构建 resq
    LOG(LogLevel::DEBUG) << "进入到了注册的模块" << req.Path() << ", " << req.Args();
}
void Search(httpRuest &req, httpResponse &resq)
{
    // 根据req, 动态的构建 resq
    LOG(LogLevel::DEBUG) << "进入到了查找的模块" << req.Path() << ", " << req.Args();
}
void Test(httpRuest &req, httpResponse &resq)
{
    // 根据req, 动态的构建 resq
    LOG(LogLevel::DEBUG) << "进入到了测试的模块" << req.Path() << ", " << req.Args();
}
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cout << "Usage error: " << argv[0] << std::endl;
        return 1;
    }
    Uport = argv[1];
    auto httpserver = std::make_shared<httpServer>(std::stoi(argv[1]));
    // 要让这个服务器具有登录功能
    httpserver->Register("/login", Login);
    httpserver->Register("/register", Register);
    httpserver->Register("/search", Search);
    httpserver->Register("/test", Test);
    httpserver->start();

    return 0;
}